﻿using System.Linq;
using System.Web.Mvc;
using TiendaMusicaWebV2.Models;
using TiendaMusicaWebV2.ViewModels;

namespace TiendaMusicaWebV2.Controllers
{
    public class ShoppingCartController : Controller
    {
        MusicStoreEntities storeDB = new MusicStoreEntities();

        public ActionResult Index()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

           
            var viewModel = new ShoppingCartViewModel
            {
                CartItems = cart.GetCartItems(),
                CartTotal = cart.GetTotal()
            };

            
            return View(viewModel);
        }

        
        public ActionResult AddToCart(int id)
        {

            
            var addedAlbum = storeDB.Albums
                .Single(album => album.AlbumId == id);

            
            var cart = ShoppingCart.GetCart(this.HttpContext);

            cart.AddToCart(addedAlbum);
                       
            return RedirectToAction("Index");
        }

               
        [ChildActionOnly]
        public ActionResult CartSummary()
        {
            var cart = ShoppingCart.GetCart(this.HttpContext);

            //ViewData["CartCount"] = cart.GetCount();

            return PartialView("CartSummary");
        }
    }
}